<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="88px">
      <el-form-item label="设备名称" prop="terminalName">
        <el-input
          v-model="queryParams.terminalName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="terminalNo">
        <el-input
          v-model="queryParams.terminalNo"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属代理商" prop="agentUid">
        <el-select v-model="queryParams.agentUid" clearable>
          <el-option
            v-for="item in agentList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备状态" prop="online">
         <el-select v-model="queryParams.online" clearable>
          <el-option
            v-for="item in onlineList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理时间">
         <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="日志状态" prop="status">
         <el-select v-model="queryParams.status" clearable>
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称"  prop="terminalName" />
      <el-table-column label="设备编号"  prop="terminalNo" />
      <el-table-column label="所属代理商"  prop="nickName" />
      <el-table-column label="总容量"  prop="capacity" >
        <template slot-scope="scope">
          <span>{{scope.row.capacity + 'L'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前剩余"  prop="currentCapacity">
        <template slot-scope="scope">
          <span>{{scope.row.currentCapacity + 'L'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余百分比" >
        <template slot-scope="scope">
          <span>{{scope.row.percent * 100 + '%'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" >
        <template slot-scope="scope">
          <span style="color:#3FE46D" v-show="scope.row.online == '1'">在线</span>
          <span style="color:#FF5555" v-show="scope.row.online == '0'">离线</span>
        </template>
      </el-table-column>
      <el-table-column label="预警状态"  >
        <template slot-scope="scope">
          <div v-if="scope.row.online == '1'">
            <div v-show="percent >= 5 && percent < 10" class="circle" style="background:#FAC818"></div>
            <div v-show="percent >= 0 && percent < 5" class="circle" style="background:#FF5555"></div>
            <div v-show="percent >= 10" class="circle" style="background:#3FE46D"></div>
          </div>
          <div v-else>
            <div class="circle" style="background:#D0D0D0"></div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="处理时间"  prop="updatedTime" />
      <el-table-column label="日志状态" >
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">未处理</span>
          <span v-else>已处理</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" />
  </div>
</template>

<script>
import { getNoticeList,getAgentList } from "@/api/common";

export default {
  name: "Online",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{}
      },
      onlineList:[
        {value:'1',label:'在线'},
        {value:'0',label:'离线'}
      ],
      statusList:[
        {value:'1',label:'已处理'},
        {value:'0',label:'未处理'}
      ],
      dateRange:[],
      agentList:[]
    };
  },
  created() {
    this.getAgent()
    this.getList();
  },
  watch:{
    dateRange(val){
      console.log(val)
      if(val){
        this.queryParams.params.startDate = val[0]
        this.queryParams.params.endDate = val[1]
      }else{
        this.queryParams.params = {}
      }
    }
  },
  methods: {
    getAgent(){
      let req = {
        userType:'01'
      }
      getAgentList(req).then(res=>{
        if(res.code == 200){
          this.agentList = res.rows.map(e=>{
            return{
              label:e.nickName,
              value:e.userId
            }
          })
        }
      })
    },
    getList() {
      this.loading = true;
      getNoticeList(this.queryParams).then(response => {
        this.list = response.rows.map(item=>{
          return {
            ...item,
            percent:item.currentCapacity / item.capacity
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams =  {
        pageNum: 1,
        pageSize: 10,
        params:{}
      }
      this.dateRange = []
      this.handleQuery();
    }
  }
};
</script>

<style lang="scss" scoped>
.circle{
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.terminalContent{
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  .terminalItem{
    width: 50%;
  }
}
.dealContent{
  display: flex;
  align-items: center;
  height: 40px;
}
</style>

